package com.hypo.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hypo.business.domain.dto.PageResultDto;
import com.hypo.business.domain.dto.WarehouseAreaOpDto;
import com.hypo.business.domain.dto.WarehouseAreaPageDto;
import com.hypo.business.domain.po.Warehouse;
import com.hypo.business.domain.po.WarehouseArea;
import com.hypo.business.domain.po.WarehouseAreaLocal;
import com.hypo.business.mapper.WarehouseAreaLocalMapper;
import com.hypo.business.mapper.WarehouseAreaMapper;
import com.hypo.business.mapper.WarehouseMapper;
import com.hypo.business.service.WarehouseAreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hypo
 * @version 1.0
 * @since 2025/8/13
 */
@Service
@RequiredArgsConstructor
public class WarehouseAreaServiceImpl extends ServiceImpl<WarehouseAreaMapper, WarehouseArea> implements WarehouseAreaService {
    private final WarehouseAreaMapper warehouseAreaMapper;

    private final WarehouseMapper warehouseMapper;

    private final WarehouseAreaLocalMapper warehouseAreaLocalMapper;

    /**
     * 根据仓库id查询库区
     */
    @Override
    public List<WarehouseArea> queryWarehouseAreaById(Integer warehouseId) {
        return warehouseAreaMapper.selectList(Wrappers.<WarehouseArea>lambdaQuery()
                .eq(WarehouseArea::getWarehouseId, warehouseId));
    }

    /**
     * 查询所有库区
     */
    @Override
    public List<WarehouseArea> queryWarehouseArea() {
        return warehouseAreaMapper.selectList(Wrappers.<WarehouseArea>lambdaQuery()
                .eq(WarehouseArea::getIsDel, 1));
    }

    /**
     * 删除库区
     */
    @Override
    public int warehouseAreaDelete(Integer id) {
        List<WarehouseAreaLocal> warehouseAreaLocals = warehouseAreaLocalMapper.selectList(Wrappers.<WarehouseAreaLocal>lambdaQuery()
                .eq(WarehouseAreaLocal::getWarehouseAreaId, id));
        if (ObjectUtil.isEmpty(warehouseAreaLocals)) {
            return warehouseAreaMapper.deleteById(id);
        }
        return 0;
    }

    /**
     * 修改库区
     */
    @Override
    public void warehouseAreaUpdate(WarehouseAreaOpDto dto) {
        WarehouseArea warehouseArea = new WarehouseArea();
        BeanUtil.copyProperties(dto, warehouseArea);
        warehouseArea.setUpdateTime(LocalDateTime.now());
        warehouseAreaMapper.updateById(warehouseArea);
    }

    /**
     * 添加库区
     */
    @Override
    public void warehouseAreaAdd(WarehouseAreaOpDto dto) {
        WarehouseArea warehouseArea = new WarehouseArea();
        BeanUtil.copyProperties(dto, warehouseArea);
        warehouseArea.setCreateTime(LocalDateTime.now());
        warehouseArea.setUpdateTime(LocalDateTime.now());
        warehouseAreaMapper.insert(warehouseArea);
    }

    /**
     * 库区分页查询
     */
    @Override
    public PageResultDto<WarehouseArea> warehouseAreaPage(WarehouseAreaPageDto dto) {
        Page<WarehouseArea> page = new Page<>(dto.getPage(), dto.getPageSize());
        Page<WarehouseArea> pageInfo = warehouseAreaMapper.selectPage(page, Wrappers.<WarehouseArea>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(dto.getWarehouseId()), WarehouseArea::getWarehouseId, dto.getWarehouseId())
                .like(ObjectUtil.isNotEmpty(dto.getName()), WarehouseArea::getName, dto.getName()));
        List<WarehouseArea> warehouseAreas = pageInfo.getRecords();
        warehouseAreas.forEach(warehouseArea -> {
            Warehouse warehouse = warehouseMapper.selectById(warehouseArea.getWarehouseId());
            warehouseArea.setWarehouseName(warehouse.getName());
        });
        return new PageResultDto<>(pageInfo.getTotal(), warehouseAreas);
    }

}
